package com.exam.task;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.exam.pojo.domain.Ranking;
import com.exam.utils.NickNameUtil;

import cn.hutool.core.util.RandomUtil;

@Component
public class RankingTask {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 每月末生成排名前一百的数据
     */
    @Scheduled(cron = "0 0 0 1 * ?")
    //@Scheduled(cron = "0/10 * * * * ?")
    public void generateRankingList(){
        //删除所有排行榜数据
        mongoTemplate.remove(new Query(), Ranking.class);
        List<Ranking> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            Ranking ranking = new Ranking();
            ranking.setId(UUID.randomUUID().toString());
            ranking.setNickName(NickNameUtil.getNickName());
            //0-100随机一个数字
            ranking.setHeadImg(RandomUtil.randomInt(1, 105)+"");
            ranking.setRate(100.0);
            ranking.setNums(30);
            //1分钟23秒 到 2分钟43秒之间随机生成一个时间的毫秒值
            ranking.setDuration(RandomUtil.randomLong(1*60+23, 2*60+43));
            list.add(ranking);
        }
        //批量插入
        mongoTemplate.insertAll(list);
    }
}
